package com.example.partyplaner;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TaskListActivity extends ListActivity implements AsyncResponse{

	List<TaskList> tasksToDo;
	ArrayList<TaskList> tasksListGet;
	ArrayList<Integer> organizerTask;
	ArrayAdapter<TaskList> adapter;
	static String value;
	private String responseMsg;
	private int responseCode;
	String taskListID;
	DatabaseConnection db;
	int getTasks;
	int logout=0;
	String filename="Tasks";
	FileOutputStream outputStream;
	FileInputStream inputStream;
	int start=0;
	int tasklists=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tasks);
		tasksListGet=new ArrayList<TaskList>();
		
		tasksToDo=new ArrayList<TaskList>();
		taskListID=null;
		getTasks=0;
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			
			tasksListGet =(ArrayList<TaskList>)extras.getSerializable("TASK_LIST");
			adapter = new ArrayAdapter<TaskList>(this,android.R.layout.simple_list_item_1, tasksListGet);
			setListAdapter(adapter);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tasks, menu);
		return true;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Intent intent = new Intent(getApplicationContext(),TasksOrganizerActivity.class);
		TaskList tasklist=(TaskList) this.getListAdapter().getItem(position);
		intent.putExtra("TASK_list1", tasklist);
		startActivity(intent);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.logout) {
			this.db = new DatabaseConnection(TaskListActivity.this);
			this.db.delegate = TaskListActivity.this;
			db.execute("http://partyplanner.mais2014.cloudbees.net/rest/WebService/Logout", "GET", MainActivity.token);
			logout=1;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void processFinish(WebserviceResponse output) {
		if(output!=null){
			responseMsg=output.getResponseMsg();
			responseCode=output.getResponseCode();
			Log.i("PartyPlanner", "process Finish");
			if (responseCode==200){
				if (logout==1)
				{
					Intent intent = new Intent(getApplicationContext(),MainActivity.class);
					startActivity(intent);
				}
			}
			else{
				Log.i("PartyPlanner", "Fail");
			}	
		}
		else{
			Log.i("PartyPlanner", "NULL");
		}
	}
	
	public Dialog onCreateDialog(int dialogId) {
		switch (dialogId) {
			case MainActivity.PLEASE_WAIT_DIALOG:
				ProgressDialog dialog = new ProgressDialog(TaskListActivity.this);
				dialog.setTitle("Connecting");
				dialog.setMessage("Please wait....");
				dialog.setCancelable(true);
				return dialog;
			default:
				break;
		}
		return null;
	}
}
